package com.example.demo.service;

import com.example.demo.mapper.MenuMapper;
import com.example.demo.mapper.RoleMapper;
import com.example.demo.mapper.RoleMenuMapper;
import com.example.demo.pojo.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

@Service
public class RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleMenuMapper role_MenuMapper;

    @Autowired
    private MenuMapper menuMapper;

    //查询所有
    public List<Role> getAllRole(){

        RoleExample roleExample = new RoleExample();

        return roleMapper.selectByExample(roleExample);

    }

    //多条件分页查询
    public PageInfo<Role> findByName(Integer pageNum, Integer pageSize, String name){

        //多条件
        RoleExample roleExample = new RoleExample();

        //创建条件容器
        RoleExample.Criteria criteria = roleExample.createCriteria();

        //模糊查询条件
        //添加按资源名字查询方法
        //resourceName.trim()去除字符串头尾空格
        if (!"".equals(name.trim()) && name.trim() != null){
            criteria.andR_nameLike("%"+name.trim()+"%");
        }

        //PageHelper分页代码时的PageHelper.startPage(pageNum,pageSize);必须写在查询方法的上一句，中间不能添加任何代码，否则失效
        PageHelper.startPage(pageNum,pageSize);
        List<Role> roles = roleMapper.selectByExample(roleExample);
        if (roles != null){
            return new PageInfo<>(roles);
        }

        return null;
    }

    //用于唯一性验证
    public List<Role> compare(Role role){

        return roleMapper.compare(role);
    }

    //添加数据
    public int addRole(Role role){

        //涉及到多表的操作
        return roleMapper.insert(role);
    }

    //添加中间表数据
    public int addRole_Menu(RoleMenu roleMenu){

        return role_MenuMapper.insert(roleMenu);
    }

    //通过m_id查询menu表信息
    public Menu getMenuById(int id){

        return menuMapper.selectByPrimaryKey(id);

    }

    //获取所有menu
    public List<Menu> getAllMenu(){

        return menuMapper.getIdAndCName();
    }

    //根据id获取角色信息
    public Role getInfoById(int roleId){

        return roleMapper.selectByPrimaryKey(roleId);
    }

    //更新角色表信息
    public int updateRole(Role role){

        return roleMapper.updateByPrimaryKey(role);

    }


    //通过role_Id删除role_menu中间表数据
    public int delRole_Menu_By_RoleId(int roleId){

        RoleMenuExample roleMenuExample = new RoleMenuExample();

        RoleMenuExample.Criteria criteria = roleMenuExample.createCriteria();
        //创建SQL执行条件where role_id = #{roleId}
        criteria.andR_idEqualTo(roleId);

        return role_MenuMapper.deleteByExample(roleMenuExample);

    }

    //获取中间表信息，通过role_id
    public List<RoleMenu> getRoleMenuByRoleId(int roleId){
        RoleMenuExample roleMenuExample = new RoleMenuExample();
        RoleMenuExample.Criteria criteria = roleMenuExample.createCriteria();
        //创建查询条件
        criteria.andR_idEqualTo(roleId);

        return role_MenuMapper.selectByExample(roleMenuExample);
    }


    //通过Role_Id删除角色信息
    public int deleteRole(int roleId){

        return roleMapper.deleteByPrimaryKey(roleId);
    }


}
